package com.shop.api;

import com.shop.config.annotation.TokenToUser;
import com.shop.entity.User;
import com.shop.util.Result;
import com.shop.util.ResultGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@RestController
@RequestMapping("/api")
public class CartApi {

    private Map<Long,String> map=new ConcurrentHashMap<>();

    @PostMapping("/cart")
    public Result addCart(@RequestParam String str, @TokenToUser User loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genFailResult(50003);//token失效
        }
        map.put(loginUser.getUserId(),str);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/cart")
    public Result getCart( @TokenToUser User loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genFailResult(50003);//token失效
        }
        return ResultGenerator.genSuccessResult(map.get(loginUser.getUserId()));
    }
}
